package com.luoyulin.sort;

/**
 * 简单选择排序
 */
public class SimpleSelectSort {

    public static void main(String[] args) {
        int[] test = {1,4,3,5,2};
        insertSort(test);
        for(int i = 0; i< test.length;i++){
            System.out.println(test[i]);
        }
    }

    /**
     * 简单选择排序
     * @param nums
     */
    public static void selecttionSort(int[] nums){
        for (int i = 0; i < nums.length; i++) {
            int minValIndex = i;
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] > nums[j]){
                    minValIndex = j;
                }
            }
            int temp = nums[i];
            nums[i] = nums[minValIndex];
            nums[minValIndex] = temp;
        }
    }

    /**
     * 冒泡排序
     * @param nums
     */
    public static void bubbleSort(int[] nums){
        for (int i = 0; i < nums.length; i++) {
            for(int j = nums.length  -1;j > i;j--){
                if(nums[j] > nums[i]){
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
    }

    public static void insertSort(int[] nums){
        for(int i = 1; i < nums.length;i++){
            for(int j = i;j > 0;j--){
                if(nums[j - 1] > nums[j]){
                    swap(nums,j-1,j);
                }else{
                    break;
                }
            }
        }
    }


    public static void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }



}
